package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.dao.DoctorDao;
import com.hospital.pojo.Doctor;
import com.hospital.service.DoctorService;
import com.hospital.utils.ShiroMd5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.Doc;

/**
 * @Author:晓风残月Lx
 * @Date: 2022/11/19 10:57
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorDao, Doctor> implements DoctorService {
    @Resource
    private DoctorDao doctorDao;

    // 通过用户名查信息
    @Override
    public Doctor getUserInfoByName(String userName) {
        return doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().eq(Doctor::getUserName,userName));
    }

    // 通过用户名查状态
    @Override
    public boolean selectByStatus(String userName) {
        Doctor doctor = doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().select(Doctor::getStatus).eq(Doctor::getUserName, userName));
        if (doctor.getStatus() == 1){
            return true;
        }
        return false;
    }

    // 通过用户名查用户权限
    @Override
    public String getUserRoleInfo(String principal) {
        return (doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().select(Doctor::getPerms).eq(Doctor::getUserName,principal))).getPerms();
    }

    // 通过用户名查用户
    public Doctor getDoctor(String userName){
        return doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().eq(Doctor::getUserName,userName));
    }

    @Override
    public Page<Doctor> getList(Page<Doctor> doctorPage) {
        return doctorDao.selectPage(doctorPage,null);
    }

    // 通过用户名或真名查询+
    @Override
    public Page<Doctor> getListBySearchName(Page<Doctor> doctorPage, String searchName) {
        return doctorDao.selectPage(doctorPage,Wrappers.<Doctor>lambdaQuery().like(Doctor::getUserName,searchName).or().like(Doctor::getRealName,searchName));
    }

    @Override
    public Doctor getDoctorByInfo(String userName) {
        return doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().eq(Doctor::getUserName,userName));
    }

    @Override
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    @Override
    public int changeStatus(Integer id,Integer status) {
        LambdaUpdateWrapper<Doctor> updateWrapper = new LambdaUpdateWrapper<>();
        if (status == 1){
            updateWrapper.set(Doctor::getStatus, 0).eq(Doctor::getId, id);
            if (this.update(null, updateWrapper)) {
                return 0;
            }
        }else if (status == 0){
            updateWrapper.set(Doctor::getStatus, 1).eq(Doctor::getId, id);
            if (this.update(null, updateWrapper)) {
                return 1;
            }
        }
        return 500;
    }

    @Override
    public boolean updateDoc(Doctor doctor) {

            if (doctor.getPassword().equals(doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().select(Doctor::getPassword).eq(Doctor::getUserName,doctor.getUserName())).getPassword())){
                try {
                    int update = doctorDao.updateById(doctor);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }else {
                doctor.setPassword(ShiroMd5Util.SysMd5(doctor.getUserName(),doctor.getPassword()));
                try {
                    int update = doctorDao.updateById(doctor);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
    }

    @Override
    public Doctor getOneById(Integer id) {
        return doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().eq(Doctor::getId,id));
    }

    @Override
    public boolean updatePerson(Long id, String username, String password, String realname, String image) {
        LambdaUpdateWrapper<Doctor> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Doctor::getRealName,realname).set(Doctor::getImage,image);

        if (password.equals(doctorDao.selectOne(Wrappers.<Doctor>lambdaQuery().select(Doctor::getPassword).eq(Doctor::getUserName,username)).getPassword())){
            updateWrapper.eq(Doctor::getUserName,username);
        }else{
            updateWrapper.set(Doctor::getPassword,ShiroMd5Util.SysMd5(username,password)).eq(Doctor::getUserName,username);
        }
        System.out.println(ShiroMd5Util.SysMd5(username,password));
        int update = doctorDao.update(null, updateWrapper);
        if (update > 0){
            return true;
        }else{
            return false;
        }
    }


}
